package com.group.itms.routes.activities;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.group.itms.routes.R;
import com.group.itms.routes.data.BObject;
import com.group.itms.routes.data.BObjectOption;
import com.group.itms.routes.data.Option;
import com.j256.ormlite.dao.Dao;

public class RouteActivity extends BaseActivity implements OnClickListener {

	private BObject mRoute;

	public static Intent getIntent(Context c, int routeId) {
		Intent intent = new Intent(c, RouteActivity.class);
		intent.putExtra(OBJECT_ID, routeId);
		return intent;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_routes);
		Bundle args = getIntent().getExtras();
		int routeId = args.getInt(OBJECT_ID);
		mRoute = getObject(routeId);
		
		String locName = args.getString("locName");
		if (locName != null) {
			setTitle(locName);
		}

		ArrayAdapter<Option> optionAdapter = new ArrayAdapter<Option>(this,
				R.layout.textview, new ArrayList<Option>());
		List<Option> options = mRoute.getOptions();
		for(Option opt : options) {
			if(!opt.getName().equals("Heights") && !opt.getName().equals("Length")){
				optionAdapter.add(opt);
			}
			else{
				TextView opt1 = ((TextView)findViewById(R.id.optionName1));
				TextView opt2 = ((TextView)findViewById(R.id.optionName2));
				BObjectOption objopt = getOO(opt); // XXX agi raia?
				if(objopt == null)
					continue;
				if(opt1.getText().equals(""))
					opt1.setText(opt.getName() +" : " + getOO(opt).getText()+ ";");
				else
					opt2.setText(opt.getName() +" : " + getOO(opt).getText()+ ";");
			}
		}
		ListView optionsListView = (ListView) findViewById(R.id.options);
		optionsListView.setAdapter(optionAdapter);
		optionsListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				startActivity(InfoDetailActivity.getIntent(RouteActivity.this, mRoute.getId(), position), true);
			}
		});
		TextView title = (TextView) findViewById(R.id.route_title);
		title.setText(mRoute.getNameInd());

		((TextView) findViewById(R.id.info_points)).setOnClickListener(this);
		((TextView) findViewById(R.id.tasks)).setOnClickListener(this);
		((TextView) findViewById(R.id.topics)).setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.menu_show_on_map, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
			switch (item.getItemId()) {
			case R.id.show_on_map:
				startActivity(NewMapActivity.getIntent(this, mRoute.getId()));
				break;
			}
		return super.onOptionsItemSelected(item);
	}
	
	private BObjectOption getOO(Option opt) {
		Dao<BObjectOption, Integer> boptDao = getHelper().getObjectOptionDao();
		try {
			return boptDao.queryBuilder()
					.where()
					.eq(BObjectOption.BOBJECT_COLUMN_NAME, mRoute)
					.and()
					.eq(BObjectOption.OPTION_COLUMN_NAME, opt).queryForFirst();
		} catch (SQLException e) {
			throw new RuntimeException();
		}
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.info_points:
			startActivity(InformationPointListActivity.getIntent(this, mRoute.getId()), true);
			break;
		case R.id.tasks:
			startActivity(TasksActivity.getIntent(this, mRoute.getId()), true);
			break;
		case R.id.topics:
			startActivity(TopicsActivity.getIntent(this, mRoute.getId()), true);
			break;
		}
		
	}

	@Override
	protected String getTag() {
		return "RouteActity";
	}

}
